package cn.ucox.web.ms.log;

import cn.ucox.web.ms.redis.RedisClient;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chenw
 * @create 2017-07-07 16:12
 * @email javacspring@gmail.com
 */
public class DexLog {

    private static final Logger logger = LoggerFactory.getLogger(DexLog.class);
    private static final String DEX_LOG = "DEX:LOG";

    private static RedisClient redisClient = RedisClient.instance();

    public static void info(String msg, Module module) {
        redisClient.lpush(DEX_LOG, new JSONObject() {{
            put("msg", msg);
            put("level", "info");
            put("mod", module.value());
            put("time", System.currentTimeMillis());
        }}.toJSONString());
    }

    public static void warn(Module module, String msg, String data) {
        redisClient.lpush(DEX_LOG, new JSONObject() {{
            put("msg", msg);
            put("level", "warn");
            put("mod", module.value());
            put("time", System.currentTimeMillis());
            put("data", data);
        }}.toJSONString());
    }

    public static void err(String msg, Module module, Exception ex) {
        redisClient.lpush(DEX_LOG, new JSONObject() {{
            put("msg", msg);
            put("level", "err");
            put("mod", module.value());
            put("time", System.currentTimeMillis());
        }}.toJSONString());
    }

    public enum Module {
        LOADER(1), FTP(2), PKG(3), MQ(4);
        private int value = 1;

        Module(int value) {
            this.value = value;
        }

        public static Module valueOf(int value) {
            switch (value) {
                case 1:
                    return LOADER;
                case 2:
                    return FTP;
                case 3:
                    return PKG;
                case 4:
                    return MQ;
                default:
                    return null;
            }
        }

        public int value() {
            return this.value;
        }
    }
}
